package com.zpark.boot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @PackageName: com.zpark.boot.controller
 * @ClassName: RedisController
 * @Description:
 * @author: RZS
 * @date: 2024/4/2  9:52
 */
@RestController
@RequestMapping("/redis")
public class RedisController {

    // 自动注入RedisTemplate
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/set")
    public void redisString(String key, String value){
        // 将数据添加到redis当中
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        // 将数据设置到redis当中（String类型）
        ops.set(key, value);
    }

    @GetMapping("/get")
    public String get(String key){
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String res = (String) ops.get(key);

        return res;
    }

    /************************ 添加list类型数据 ************************/
    @PostMapping("/lpush")
    public void lpush(@RequestBody String[] lt){
        ListOperations ops = redisTemplate.opsForList();

        for (String s : lt) {
            ops.leftPush("lt", s);
        }
    }

    // 获取list类型数据
    @GetMapping("/lrange")
    public List lrange(){
        ListOperations ops = redisTemplate.opsForList();
        List lt = ops.range("lt", 0, 10);

        return lt;
    }

    public void test(){
        // 操作hash
        HashOperations hash = redisTemplate.opsForHash();
        // hash.put(); hset 命令
        // hash.get() hget命令
        // hash.values()  获取所有的value
        // hash.keys() 获取所有的key——field
    }

}
